package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetCurrencyConfigResp;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.OrderProductListBean;
import com.shop2cn.iapi.agentproducts.parameter.OrderProductListDto;
import com.shop2cn.iapi.agentproducts.parameter.SaveSelectionConditionBean;
import com.shop2cn.iapi.agentproducts.service.OrderProductListCall;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.iapi.productsynces.parameter.es.EsGodProduct;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
 * 代下单商品列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_OrderProductList {
	private static OrderProductListBean orderproductlistBean;
	private static OrderProductListCall orderproductlistCall;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// 渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";
	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	// vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代下单商品列表");
	}

	@Before
	public void caseUp() {
		orderproductlistBean = new OrderProductListBean();
		orderproductlistCall = new OrderProductListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(orderproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(OrderProductListBean orderproductlistBean) throws InterruptedException {
		Thread.sleep(2000);
		YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
		YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
		List<OrderProductListDto> orderProductListDtos = orderproductlistCall.getProductList();
		orderProductListDtos.forEach(p -> {
			if (orderproductlistBean.getTitle() != null) {
				Logger.verifyEquals(true, p.getTitle().contains(orderproductlistBean.getTitle()), "验证传入参数title");
			}
			try {
				String rootProductId = ymtProductsIWapper.selectAgentProductByProductId(p.getProductId())
						.getRootProductId();
				YmtProductsI ymtProductsI = ymtProductsIWapper.selectProductByProductId(p.getProductId()).get(0);
				Logger.verifyEquals(p.getTitle(), ymtProductsI.getsProduct(), "验证title");
				Logger.verifyEquals(p.getProductPic().replaceAll("lb.jpg", "l.jpg"), ymtProductsI.getsPicUrl(), "验证productPic");
				Logger.verifyEquals(p.getBrandName(),
						ymtProductsIWapper.selectProductByProductId(rootProductId).get(0).getsBrand(), "验证brandName");
				GetCurrencyConfigResp currencyConfigResp = AgentProductCallService
						.getCurrencyConfigResp(p.getCurrency().getCode());
				Logger.verifyEquals(p.getCurrency().getCode(), currencyConfigResp.getCurrency(), "验证code");
				Logger.verifyEquals(p.getCurrency().getName(), currencyConfigResp.getName(), "验证name");
				Logger.verifyEquals(p.getCurrency().getSymbol(), currencyConfigResp.getMark(), "验证mark");
//				Logger.verifyEquals(p.getCurrency().getExchangeRate(), currencyConfigResp.getExchangeRmbRate(),
//						"验证ExchangeRmbRate");
				List<Double> prices = new ArrayList<>();
				p.getCatalogList().forEach(x -> {
					try {
						YmtCatalogsI ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByCatalogId(x.getCatalogId());
						ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByCatalogId(x.getCatalogId());
						List<Map> maps = ymtCatalogsIWapper.selectCatalogSetAttributesByCatalogId(x.getCatalogId());
						List<String> sCategoryAttribute = maps.stream().map(o -> o.get("sCategoryAttribute").toString())
								.collect(Collectors.toList());
						// String s = sCategoryAttribute.get(0);
						for (int i = 0; i < sCategoryAttribute.size(); i++) {
							// s = s+"+"+sCategoryAttribute.get(i);
							Logger.verifyIsContains(sCategoryAttribute.get(i), x.getPropertyValue(), "验证propertyValue");
						}
						// Logger.verifyEquals(x.getPropertyValue(),s,"验证propertyValue");
						Logger.verifyEquals(x.getSalePrice(), ymtCatalogsI.getfQuotePrice().doubleValue(),
								"验证salePrice");
						prices.add(x.getSalePrice());
						YmtCatalogsI ymtCatalogsI1 = ymtCatalogsIWapper
								.selectCatalogsByCatalogId(ymtCatalogsI.getRootCatalogId());
						Logger.verifyEquals(x.getStock(), ymtCatalogsI1.getiNum(), "验证stock");
						System.out.println(ymtCatalogsI);
						Logger.verifyEquals(x.getSku(), ymtCatalogsI.getsSKU(), "验证sku");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				});
				Logger.verifyEquals(p.getMinPrice(), Collections.min(prices), "验证minPrice");
				AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(p.getProductId());
				Logger.verifyEquals(p.getAgentType(), agentProductPo.getAgentType(), "验证agentType");
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		});

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_01() {
		Logger.start(true, "代下单商品列表，渠道商获取全部商品列表，获取成功");
		try {
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(rootUserId);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			verifyDB(orderproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_02() {
		Logger.start(true, "代下单商品列表，渠道商根据二级类目获取商品列表，获取成功");
		try {
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(rootUserId);
			orderproductlistBean.setSecondCategoryId(1002);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");

			verifyDB(orderproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_03() {
		Logger.start(true, "代下单商品列表，根据商品标题获取商品列表，获取成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.setTitle("happ" + TestDataManager.getRandomNum(7));
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(rootUserId);
			orderproductlistBean.setTitle("happ");
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			verifyDB(orderproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_04() {
		Logger.start(true, "代下单商品列表，userId为空");
		try {
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(0);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("400", orderproductlistCall.getString("code"), "验证code");
			Logger.verifyEquals(true, orderproductlistCall.getJsonObject().get("data").isJsonNull(), "验证data为空");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_06() {
		Logger.start(true, "代下单商品列表，代理商获取商品列表，获取成功");
		try {
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(agentUserId1);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
//			verifyDB(orderproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_07() {
		Logger.start(true, "代下单商品列表，渠道商修改品牌品类，获取代理商代下单列表");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, vipAgentUserName1, productId, vipRootUserId, true);
			String productId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId2, vipAgentUserName2, productId1, vipAgentUserId1, false);
			importAgentProductBean1.setTitle("22");
			String productId2 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);

			ModifyAgentProductBean modifyAgentProductBean = AddAgentProductCallService
					.initModifyAgentProductBean(productId, addAgentProductBean);
			modifyAgentProductBean.setBrandId(10065);
			modifyAgentProductBean.setCategoryId(1006);
			modifyAgentProductBean.setThirdCategoryId(1012);
			AddAgentProductCallService.modifyAgentProduct(modifyAgentProductBean);

			Thread.sleep(2000);
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(vipAgentUserId2);
			orderproductlistBean.setTitle(importAgentProductBean1.getTitle());
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			verifyDB(orderproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_08() {
		Logger.start(true, "代下单商品列表，渠道商获取全部商品列表，以及税价、获取成功");
		try {
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(AgentUsers.vipRootUserId);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			verifyDB(orderproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_09() {
		Logger.start(true, "通过es获取");
		try {
			// 发布商品1
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			addAgentProductBean.setTitle("t恤女长袖白色纯棉宽松女装内搭打底衫2019新款秋季上衣初秋ins潮");
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			Thread.sleep(5000);

			// 查询es，判断同步
			EsGodProduct esProduct = Ts_GetProductList.checkEsSync(productId);
			
			// 模糊查询
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(vipRootUserId);
			orderproductlistBean.setTitle("女装");
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			verifyDB(orderproductlistBean);
			List<OrderProductListDto> productList = orderproductlistCall.getProductList();
			// 判断是否查询到了
			Logger.verifyNotEquals("-1", productList.toString().indexOf(productId)+"", "没有查询到刚刚添加的商品");

			// 打乱查询
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(vipRootUserId);
			orderproductlistBean.setTitle("女装内搭打底衫纯棉袖新款秋季白色宽松2019");
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			 productList = orderproductlistCall.getProductList();

			// 判断是否查询到了
//			Logger.verifyNotEquals(-1, productList.toString().indexOf(esProduct.getPid()), "没有查询到刚刚添加的商品");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_010() {
		Logger.start(true, "代下单商品列表，查看费率");
		try {
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(501150255);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			Logger.verifyEquals(15.00, orderproductlistCall.getJsonObject().getAsJsonObject("data").get("rate").getAsDouble(), "验证手续费");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_011() {
		Logger.start(true, "商户-》代理，条件筛选不匹配查不到，不会自动铺货、然后删除商品");
		try {
			Integer MchId = GodmchManageCallService.CreateGodMch();
            Integer agenttype=GodmchManageCallService.getAgentType(MchId, 1);
            Integer AgentId = GodmchManageCallService.getAgentMchId(MchId, agenttype);
            //添加导入商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean initImportAgentProduct = AddAgentProductCallService.initImportAgentProduct(AgentId, "", productId, MchId, false);
			String AgentProductId = AddAgentProductCallService.ImportAgentProduct(initImportAgentProduct);
			
			Thread.sleep(2000);
			orderproductlistBean.setUserId(AgentId);
			orderproductlistBean.setPageIndex(1);
			orderproductlistBean.setSecondCategoryId(-1);
			orderproductlistBean.setPageSize(10);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			List<OrderProductListDto> productListDtoList2 = orderproductlistCall.getProductList();
			Logger.verifyEquals(productListDtoList2.get(0).getProductId(),AgentProductId, "验证productId");
			
			SaveSelectionConditionBean initSaveSelectionCondition = AddAgentProductCallService.InitSaveSelectionCondition(MchId, AgentId, 1);
			initSaveSelectionCondition.setBrandIdList(Arrays.asList(666));
			AddAgentProductCallService.SaveSelectionCondition(initSaveSelectionCondition);
			
			Thread.sleep(3000);
			
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			List<OrderProductListDto> productListDtoList = orderproductlistCall.getProductList();
			Logger.verifyEquals(0,productListDtoList.size(), "验证productId");
			
			// 查询代理商的商品id
			List<AgentProductPo> ap1 = ymtProductsIWapper.selectAgentProductByRootProductId(productId).stream()
								.filter(x -> x.getUserId() == AgentId)
								.collect(Collectors.toList());
			Logger.verifyEquals(-2, ap1.get(0).getAction(), "筛选后删除商品");
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OrderProductList_012() {
		Logger.start(true, "返回增加品牌id字段");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "", true);
			addAgentProductBean.setBrandId(21477);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			orderproductlistBean = AddAgentProductCallService.initOrderProductListBean(rootUserId);
			orderproductlistCall.setData(orderproductlistBean);
			orderproductlistCall.callService();
			Logger.verifyEquals("200", orderproductlistCall.getString("code"), "验证code");
			OrderProductListDto orderProductListDto = orderproductlistCall.getProductList().get(0);
			Logger.verifyEquals(productId, orderProductListDto.getProductId(), "验证品牌id");
			Logger.verifyEquals(21477, orderProductListDto.getBrandId(), "验证品牌id");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

}